<?php
/**
 * 用户
 *
 *
 */


class restaurant_model extends CI_Model
{
	const tableName = "restaurant";

	var $name;

	var $address;

	var $coordinate;

	var $description;

	var $homepage;

	var $notice;

	var $serviceTime;

	var $promotion;
	
	var $rating;
	
	var $minimum;

	var $compensate;

	var $phone;

	var $picture;

	var $owner;

	var $ownerName;

	var $ownerMobile;

	var $ownerPhone;

	var $ownerQQ;

	var $status;

	var $regTime;


	function __construct()
	{
		parent::__construct();
	}

	// --------------------------------------------------------------------

	/**
	 *
	 * 插入user数据
	 */
	function insert_entry($data)
	{
		$this->name = $data['name'];
		$this->address = $data['address'];
		$this->coordinate = $data['coordinate'];
		$this->description = $data['description'];
		$this->homepage = $data['homepage'];
		$this->notice = $data['notice'];
		$this->serviceTime = $data['serviceTime'];
		$this->promotion = $data['promotion'];
		$this->rating = $data['rating'];
		$this->minimum = $data['minimum'];
		$this->compensate = $data['compensate'];
		$this->phone = $data['phone'];
		$this->picture = $data['picture'];
		$this->owner = $data['owner'];
		$this->ownerName = $data['ownerName'];
		$this->ownerMobile = $data['ownerMobile'];
		$this->ownerPhone = $data['ownerPhone'];
		$this->ownerQQ = $data['ownerQQ'];
		$this->status = $data['status'];
		$this->regTime = $data['regTime'];

		$this->db->insert(self::tableName, $this);
	}

	/**
	 * load by id
	 *
	 *
	 */
	function load($id)
	{
		if (!$id){
			return array();
		}

		$query = $this->db->get_where(self::tableName,array('id' => $id));

		if ($row = $query->row_array()){
			return $row;
		}

		return array();
	}

	function findByLocation($entryId){
		date_default_timezone_set('UTC');
		$this->db->select('restaurant.*,'.'distribution.distance');
		$this->db->from('restaurant');
		$this->db->join('distribution','restaurant.id=distribution.restaurantId');
		$this->db->join('location','distribution.locationId=location.id');
		$this->db->where('location.id', $entryId);
		$this->db->where('restaurant.status', 'ACTIVE');
		$query = $this->db->get();
	    $rows = array();
        foreach ($query->result_array() as $row){
        	$str = explode('-',$row['serviceTime']);
        	$row['isOpen'] = date('H:i') >= $str[0] && date('H:i') <= $str[1] ? 1 : 0;
        	$row['isCompensate'] = empty($row['compensate']) ? 0 : 1;
        	$row['isPromotion'] = empty($row['promotion']) ? 0 : 1;
            $rows[] = $row;  
        }
		return $rows;
	}
	
	function getRstsIdByLabel($labelId, $entryId){
		$rstsId = $this->_getRstsIdByLocation($entryId);
		$this->db->select('restaurantId');
		$this->db->from('filter');
		$this->db->where('labelId', $labelId);
		$this->db->where_in('restaurantId',$rstsId);
		$query = $this->db->get();
		$rows = array();
		foreach ($query->result_array() as $row){
            $rows[] = (int)$row['restaurantId'];  
        }
		return $rows;
		
	}
	
	function _getRstsIdByLocation($entryId){
		$this->db->select('restaurantId');
		$this->db->from('distribution');
		$this->db->where('locationId', $entryId);
		$query = $this->db->get();
		$rows = array();
		foreach ($query->result_array() as $row){
            $rows[] = $row['restaurantId'];  
        }
		return $rows;
	}
	
	function addFavorite(){
		
	}
	
}